package com.cennavi.mineplay.mineplay_data.model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.List;

public class ImportEventPoint {

	private Connection conn;
	
	private List<EventPoint> ps;
	
	private String destTableName;
	
	public ImportEventPoint(Connection conn, List<EventPoint> ps,String destTableName) {
		super();
		this.conn = conn;
		this.ps = ps;
		this.destTableName = destTableName;
	}



	public void importData() throws Exception{
		
		 
		
		String sql2 = "insert into "+destTableName+"(car_number,geom,alarm,time,speed) "
				+ "values (?,st_geomfromtext(?,4326),?,?,?) "
				+ "on conflict(car_number,time) do nothing";
		
		PreparedStatement pstmt =this.conn.prepareStatement(sql2);
		
		for(EventPoint tp : ps){
			pstmt.setString(1, tp.getCar_number());
			pstmt.setString(2, "Point("+ tp.getLng()+" "+tp.getLat()+")");
			pstmt.setLong(3, tp.getType());
			pstmt.setLong(4, tp.getTime());
			pstmt.setDouble(5, tp.getSpeed());
			pstmt.addBatch();
		}
		
		pstmt.executeBatch();
		
		pstmt.close();
		
//		conn.close();
	}
	
}
